package com.baomidou.kisso.service.ym.impl;
import com.baomidou.kisso.common.http.HttpHelper;
import com.baomidou.kisso.common.http.HttpResult;
import com.baomidou.kisso.common.util.MD5Util;
import com.baomidou.kisso.common.util.MsConfig;
import com.baomidou.kisso.common.util.PasswordUtil;
import com.baomidou.kisso.dto.Result;
import com.baomidou.kisso.dto.ResultGenerator;
import com.baomidou.kisso.dto.SSOAccount;
import com.baomidou.kisso.service.ym.AccountService;
import com.baomidou.kisso.service.ym.TokenService;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {
    public static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);
    @Autowired
    private MsConfig msConfig;

    @Autowired
    private TokenService tokenService;

    @Override
    public Result verification(String token,String account, String password) {
        //获取token
        if(token==null&&token.equals("")){
            return ResultGenerator.genFailResult("鉴权错误");
        }
        String  md5Password = MD5Util.md5(password);
        String url = msConfig.getIp()+msConfig.getPort()+"/account/v2/verification?access_token="+token+"&account="+account+"&password="+md5Password;
        String accountInfoUrl = msConfig.getIp()+msConfig.getPort()+"/account/info?access_token="+token+"&account="+account+"&account_uuid=";

        try {
            HttpResult httpResult = HttpHelper.executeGet(url,"UTF-8",60000);
            //Map<String, Object> data = restTemplate.getForObject(msConfig.getIp()+msConfig.getPort()+"/", Map.class, params);
            logger.info("TokenService getAccountInfo: {}", httpResult);

            if(Integer.valueOf(httpResult.getStatusCode().toString()) !=200)
                return ResultGenerator.genFailResult(httpResult.getContent());
            JSONObject json = new JSONObject(httpResult.getContent());
            if(Integer.parseInt(json.get("code").toString())!=0){
                return ResultGenerator.genFailResult(json.get("msg").toString());
            }
            JSONObject accountObject = (JSONObject) json.get("content");
            String accountUuid = accountObject.getString("accountUuid");
            //查询info
            HttpResult infoResult = HttpHelper.executeGet(accountInfoUrl+accountUuid,"UTF-8",60000);
            JSONObject infoJson = new JSONObject(infoResult.getContent());
            if(Integer.parseInt(infoJson.get("code").toString())!=0){
                return ResultGenerator.genFailResult(infoJson.get("msg").toString());
            }
            JSONObject infoObject = (JSONObject) infoJson.get("content");
            SSOAccount ssoAccount = new SSOAccount();
            ssoAccount.setAccountUuid(accountObject.getString("accountUuid"));
            ssoAccount.setAccount(accountObject.getString("account"));
            ssoAccount.setRealName(infoObject.getString("realName"));
            return ResultGenerator.genSuccessResult(ssoAccount);
        }catch (Exception e){
            logger.warn("TokenService getAccountInfo occurred exception: {}", e.getMessage(), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }





}
